// OSR b JPSERVERTHREAD
package org.jvcs.fmwk;

import org.jvcs.fmwk.*;
import org.jvcs.common.*;
import org.jvcs.utils.*;

import java.net.*;
import java.io.*;

public class JPServerThread extends Thread {
    private Socket socket = null;

    public JPServerThread(Socket socket) {
		super("JPServerThread");
		this.socket = socket;
    }

    public void run() {

		try {
		
	    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
	    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

	    String inputLine, outputLine;
	    jvcsProtocol jp = new jvcsProtocol();
		
		// First Server Says hello to Connecting client
	    outputLine = jp.Q_HELLO;
	    out.println(outputLine);

		// The communication starts
	    while ((inputLine = in.readLine()) != null) {
			osr.pl("Client "+" Request: "+inputLine);
			outputLine = jp.respond(inputLine);
			//Thread.sleep(3000);
			osr.pl("Server Response: "+outputLine);
			out.println(outputLine);
			if (outputLine.equals(jp.Q_BYE))
				break;
			}
			out.close();
			in.close();
			socket.close();

		} catch (Exception e) {
			osr.pl("Error in the run thread");
			e.printStackTrace();
		}
    }
}
